package user_view

import (
	"data_monitor/pkg/user/user_model"
	"fmt"
	"strings"
)

type CreateUserRequest struct {
	Name     *string
	Password *string
	IsAdmin  *int
}

type DeleteUserRequest struct {
	Id   *int
	Name *string
}

type UpdateUserRequest struct {
	Name     *string	`json:"name"`
	Password *string	`json:"password"`
}

func (r CreateUserRequest) CopyToModel(m *user_model.User) {
	if r.Name != nil {
		m.Name = *r.Name
	}
	if r.Password != nil {
		m.Password = *r.Password
	}
}

func (r CreateUserRequest) CheckParams() error {
	var errMsgs []string
	if r.Name == nil {
		errMsgs = append(errMsgs, fmt.Sprint("username cannot be empty string"))
	}

	if len(errMsgs) > 0 {
		return fmt.Errorf(strings.Join(errMsgs, "; "))
	}
	return nil
}

func (r UpdateUserRequest) CopyToModel(m *user_model.User) {
	if r.Name != nil {
		m.Name = *r.Name
	}
	if r.Password != nil {
		m.Password = *r.Password
	}
}

func (r UpdateUserRequest) CheckParams() error {
	var errMsgs []string
	if r.Name == nil {
		errMsgs = append(errMsgs, fmt.Sprint("username cannot be empty string"))
	}

	if len(errMsgs) > 0 {
		return fmt.Errorf(strings.Join(errMsgs, "; "))
	}
	return nil
}

type SignInRequest struct {
	Name     *string
	Password *string
}

func (r SignInRequest) CopyToModel(m *user_model.User) {
	if r.Name != nil {
		m.Name = *r.Name
	}
	if r.Password != nil {
		m.Password = *r.Password
	}
}
